﻿using CuaHangLego.DataAccess;
using CuaHangLego.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CuaHangLego.Bussiness
{
    public class UserAccess : DABase
    {
        public static KhachHang currentUser;

        public static bool Register(KhachHang info, TaiKhoan acc)
        {
            int ID = KhachHangDA.Instance.Insert(info);
            if (ID > 0) return KhachHangDA.Instance.UpdateAccount(ID, acc);
            else return false;
        }

        public static bool ChangePassword(string newPassowrd)
        {
            return currentUser != null ? KhachHangDA.Instance.ChangePassword(currentUser.MaSo, newPassowrd) : false;
        }

        public static bool CheckExistUsername(string username)
        {
            return KhachHangDA.Instance.CheckExistUsername(username);
        }

        public static bool Login(string username, string password)
        {
            // Kiem tra xem co ton tai tai khoan nay trong database
            if (!CheckExistUsername(username)) return false;
            else
            {
                int ID = KhachHangDA.Instance.GetIDLogin(username, password);
                // Neu co tai khoan thi lay doi tuong khach hang va gan cho currentUser
                if (ID > 0) currentUser = KhachHangDA.Instance.GetByID(ID);
                else return false;
            }
            return true;
        }

        public static bool CheckPassword(int ID, string password)
        {
            return KhachHangDA.Instance.CheckPassword(ID, password);
        }

        public static void Logout()
        {
            currentUser = null;
        }

        public static bool IsLogin()
        {
            return currentUser != null;
        }
    }
}
